using Blog2025.Application.Common;
using Blog2025.Application.Dto;
using Blog2025.Application.Services;
using Blog2025.Domain.Entities.App;
using Microsoft.AspNetCore.Mvc;

namespace Blog2025.Api.Controllers;
[ApiController]
[Route("api/[controller]")]
public class RoleController:ControllerBase
{
    private readonly IRoleAppServices _service;
    public RoleController(IRoleAppServices service)
    {
        _service=service;
    }
    [HttpPost]
    public async Task<IActionResult> Create(CreateRole dto)
    {
        await _service.CreateRoleAsync(dto);
        return Ok("创建成功");
    }
    [HttpDelete("{id}")]
    public async Task<IActionResult> Delete(Guid id)
    {
        await _service.DeleteRoleAsync(id);
        return Ok("删除成功");
    }
    [HttpPut("{id}")]
    public async Task<IActionResult> Update(Guid id,UpdateRoleDto dto)
    {
        await _service.UpdateRoleAsync(id,dto);
        return Ok("修改成功");
    }
    [HttpGet]
    public async Task<ApiResult> GetAll(PageRequestDto pageRequestDto)
    {
        var obj=await _service.GetAllRoleAsync(pageRequestDto);
        return ApiResult.Success(obj);
    }

    [HttpGet("filter")]
    public async Task<IActionResult>  GetFilter()
    {
        var filters=new Dictionary<string,object>
        {
            {"rolename","普通用户"}
        };
        var res=await _service.GetFilterAsync(filters);
        return Ok(res);
    }

    [HttpGet("{id}")]
    public async Task<IActionResult> GetById(Guid id)
    {
        var obj= await _service.GetByIdRoleAsync(id);
        return Ok(obj);
    }

    [HttpPut("{id}/Assign-permission")]
    // 新增权限
    public async Task<IActionResult> AssignPermission(Guid id,AssignPermissionDto dto)
    {
        var obj=await _service.AssignPermission(id,dto.Code);
        return Ok(obj);
    }
    [HttpPut("{id}/Remove-permission")]
    // 移除权限
    public async Task<IActionResult> RemovePermission(Guid id,AssignPermissionDto dto)
    {
        var obj=await _service.RemovePermission(id,dto.Code);
        return Ok(obj);
    }
}